package fourFragment.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.example.xuegengwang.xuegengwang.fragment.FourFragment;
import com.example.xuegengwang.xuegengwang.service.UpdateService;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.io.File;

import common.BaseActivity;
import common.Constant;
import common.Variable;
import https.InterNetController;
import utils.FileUtils;
import utils.SPUtils;
import utils.ToastUtils;
import utils.UpdateUtils;
import utils.VariousUtils;

/**
 * Created by Administrator on 2017/4/6.
 * 设置页
 */
@ContentView(R.layout.setting)
public class Setting extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.setting_about)
    private RelativeLayout setting_about;
    @ViewInject(R.id.setting_version)
    private RelativeLayout setting_version;
    @ViewInject(R.id.setting_suggest)
    private RelativeLayout setting_suggest;
    @ViewInject(R.id.setting_logout)
    private RelativeLayout setting_logout;
    @ViewInject(R.id.setting_version_tv)
    private TextView setting_version_tv;
    @ViewInject(R.id.setting_version_ima)
    private ImageView setting_version_ima;
    private boolean updatable;//是否可以更新
    private MyReceiver myReceiver;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        String err_code = jsonObject.getString("err_code");
                        if ("0".equals(err_code)) {
                            //退出登录成功
                            ToastUtils.shortToast("退出登录");
                            SharedPreferences.Editor edit = SPUtils.getSharedPreferences().edit();
                            edit.putString(Constant.USER_ID, "");
                            edit.commit();
                            FileUtils.getInstance().deleteDir(new File(Constant.VOLUNTEERS_DIR));//删除志愿者文件夹
                            Setting.this.setResult(FourFragment.LOGOUT_CODE, null);
                            Setting.this.finish();
                        }
                    } catch (JSONException e) {
                        ToastUtils.shortToast("退出登录失败");
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkVersion();
    }

    private void initView() {
        common_title.setText("设置");
        common_back.setOnClickListener(this);
        setting_about.setOnClickListener(this);
        setting_suggest.setOnClickListener(this);
        setting_version.setOnClickListener(this);
        setting_logout.setOnClickListener(this);
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter(Constant.ACTION_NEW_VERSION);
        registerReceiver(myReceiver, intentFilter);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            case R.id.setting_about:
                intent.setClass(this, About.class);
                startActivity(intent);
                break;
            case R.id.setting_suggest:
                intent.setClass(this, Suggestion.class);
                startActivity(intent);
                break;
            case R.id.setting_version:
                if (updatable) {
                    //可以更新
                    UpdateUtils.getInstance().updateNewVersion(Setting.this);
                }
                break;
            case R.id.setting_logout://登出
                logout();
                break;
            default:
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    private void logout() {
        new InterNetController(this, Constant.LOGOUT, handler, null, 0);
    }

    private void checkVersion() {
        if (Variable.IS_NEW_VERSION) {
            newVersion();
        } else {
            setting_version_ima.setVisibility(View.GONE);
            setting_version_tv.setText(VariousUtils.getVersion());
            updatable = false;
            Intent i = new Intent(Setting.this, UpdateService.class);
            startService(i);
        }
    }

    private void newVersion() {
        //有新版本
        setting_version_ima.setVisibility(View.VISIBLE);
        setting_version_tv.setText("有新的版本");
        updatable = true;
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Constant.ACTION_NEW_VERSION:
                    newVersion();
                    break;
                default:
                    break;
            }
        }
    }
}
